package com.art.sunflower.controller;

import com.art.sunflower.exception.NullStudentException;
import com.art.sunflower.pojo.User;
import com.art.sunflower.service.StudentService;
import com.art.sunflower.util.Message;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping(value = "/student")
@Api(value = "学生控制器")
public class StudentController {

    @Resource
    StudentService studentService;

    @RequiresRoles(value = {"admin"})
    @ApiOperation(value = "分页获取学生数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "页大小", dataType = "int", paramType = "query", defaultValue = "5"),
            @ApiImplicitParam(name = "pageNo", value = "页码", dataType = "int", paramType = "query", defaultValue = "1"),})
    @GetMapping()
    public Message getList(Integer pageSize, Integer pageNo) {
        try {
            return Message.createSuc(studentService.getList(pageSize, pageNo));
        } catch (NullStudentException e) {
            e.printStackTrace();
            return Message.createErr("学生不存在");
        }
    }

    @RequiresRoles(value = {"admin"})
    @ApiOperation(value = "增加一条学生数据")
    @PostMapping()
    public Message add(String username, String password, String phone, String name, String sex, Integer year, String parentName, Integer classroomId) {
        try {
            studentService.add(username, password, phone, name, sex, year, parentName, classroomId);
            return Message.createSuc("添加成功");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return Message.createErr("添加失败");
        }
    }

    @RequiresRoles(value = {"admin"})
    @ApiOperation(value = "修改一条学生数据")
    @PutMapping()
    public Message update(Integer id, String username, String password, String phone, String realName, String sex, Integer enteryear, String parentName, Integer classroomId) {
        String msg = "修改成功";
        try {
            studentService.update(id, username, password, phone, realName, sex, enteryear, parentName, classroomId);
            return Message.createSuc(msg);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            msg = "修改失败";
        } catch (NullStudentException e) {
            e.printStackTrace();
            msg = "学生不存在";
        }
        return Message.createErr(msg);
    }

    @RequiresRoles(value = {"admin"})
    @ApiOperation(value = "删除一条学生数据")
    @DeleteMapping(value = "/{id}")
    public Message delete(@PathVariable Integer id) {
        String msg = "删除成功";
        try {
            studentService.delete(id);
            return Message.createSuc(msg);
        } catch (NullStudentException e) {
            e.printStackTrace();
            msg = "学生不存在";
        }
        return Message.createErr(msg);
    }

    @RequiresRoles(value = {"admin"})
    @ApiOperation(value = "查看一条学生数据")
    @GetMapping(value = "/{id}")
    public Message get(@PathVariable Integer id) {
        String msg;
        try {
            return Message.createSuc(studentService.getById(id));
        } catch (NullStudentException e) {
            e.printStackTrace();
            msg = "学生不存在";
        }
        return Message.createErr(msg);
    }

    @RequiresRoles(value = {"teacher"})
    @ApiOperation(value = "查看所有行政学生")
    @GetMapping(value = "/classroomStudent")
    public Message getClassroomStudent() {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        List<Map<String, Object>> map = new ArrayList<>();
        List<Integer> teacherClassroom = studentService.getTeacherClassroomStudent(user.getId());
        for (int i = 0; i < teacherClassroom.size(); i++) {
            Map<String, Object> studentInformation = studentService.getById(teacherClassroom.get(i));
            map.get(i).put("student", studentInformation);
        }
        return Message.createSuc(map);
    }

    @RequiresRoles(value = {"teacher"})
    @ApiOperation(value = "查看所有教学班学生")
    @GetMapping(value = "/classesStudent")
    public Message getClassesStudent() {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        List<Map<String, Object>> map = new ArrayList<>();
        List<Integer> teacherClasses = studentService.getTeacherClassesStudent(user.getId());
        for (int i = 0; i < teacherClasses.size(); i++) {
            Map<String, Object> studentInformation = studentService.getById(teacherClasses.get(i));
            map.get(i).put("student", studentInformation);
        }
        return Message.createSuc(map);
    }

}
